<template>
	<view>
		<view class="m30">
			<view class="c343 bold">{{detail.title}}</view>
			<view class="f24 c999 mt10">来源：{{detail.zoneCategory.origin}}</view>
			<u-gap height="20"></u-gap>
			<view class="f26" v-if="unscramble">
				<u-parse :content="detail.unscramble"></u-parse>
			</view>
			<view class="f26" v-else>
				<u-parse :content="detail.article"></u-parse>
			</view>
			<u-gap height="100"></u-gap>
			<view class="fix-bot">
				<view class="fix-bot-box mb30 flex-au" v-if="unscramble">
					<view class="">
						<u-button color="#fff" :customStyle="{'color':'#111','padding':'0 40rpx'}" shape="circle"
							@click="unscrambleBtn(false)">返回政策详情</u-button>
					</view>
				</view>
				<view class="fix-bot-box mb30 flex-au" v-else>
					<view class="" v-if="detail.annex">
						<u-button color="#fff" :customStyle="{'color':'#111','padding':'0 40rpx'}" shape="circle"
							@click="getFile">政策文件</u-button>
					</view>
					<view class="">
						<u-button color="#0066FD" :customStyle="{'color':'#fff','padding':'0 40rpx'}" shape="circle"
							@click="unscrambleBtn(true)">政策解读</u-button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getPolicyinfo
	} from '@/common/api_things.js'
	export default {
		name: '',
		data() {
			return {
				id: '',
				detail: {},
				unscramble: false
			}
		},
		onLoad(option) {
			this.id = option.id
			this.getPolicyinfo()
		},
		methods: {
			async getPolicyinfo() {
				uni.showLoading({
					mask: true
				})
				const res = await getPolicyinfo({
					id: this.id
				})
				if (res.code == 200) {
					this.detail = res.data
					uni.setNavigationBarTitle({
						title: this.detail.title
					})
				}
				uni.hideLoading()
			},
			getFile() {
				let link = this.api_host + this.detail.annex
				uni.getSystemInfo({
					success: (res) => {
						if (res.system.includes('iOS')) {
							uni.navigateTo({
								url: '/pages/webview/index?link=' + decodeURIComponent(link)
							})
						} else {
							const downloadTask = uni.downloadFile({
								url: link,
								success: (res) => {
									const path = res.tempFilePath;
									uni.openDocument({
										filePath: path,
										fileType: 'pdf',
										success: (res) => {
											// uni.navigateBack({
											// 	delta: 1
											// });
											uni.showToast({
												title: '打开成功',
												icon: 'none',
												duration: 2000
											});
										},
										fail: (err) => {
											console.log(err, 'err');
											uni.showToast({
												title: '打开文件失败',
												icon: 'none',
												duration: 2000
											});
										}
									});
								},
								fail: (err) => {
									console.log(err, 'err');
									uni.showToast({
										title: '下载文件失败',
										icon: 'none',
										duration: 2000
									});
								}
							});
							downloadTask.onProgressUpdate(function(res) {
								uni.showLoading({
									title: '正在打开：' + res.progress + '%',
									mask: true
								})
							})
							uni.hideLoading()
						}
					}
				});
			},
			unscrambleBtn(status) {
				this.unscramble = status
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F0F7FF;
	}
</style>